////
//// Source code recreated from a .class file by IntelliJ IDEA
//// (powered by FernFlower decompiler)
////
//
//package com.cloudera.api.dao.impl;
//
//import com.cloudera.api.ApiVersionContext;
//import com.cloudera.api.dao.LicenseManagerDao;
//import com.cloudera.api.model.ApiLicense;
//import com.cloudera.api.model.ApiLicenseFeature;
//import com.cloudera.api.model.ApiLicensedFeatureUsage;
//import com.cloudera.cmf.LicenseData;
//import com.cloudera.cmf.ProductState;
//import com.cloudera.cmf.ProductState.Feature;
//import com.cloudera.cmf.license.License;
//import com.cloudera.cmf.license.TrialLicense;
//import com.cloudera.cmf.service.ServiceDataProvider;
//import com.cloudera.server.cmf.ClusterUsage;
//import com.cloudera.server.cmf.ClusterUsageCollection;
//import com.cloudera.server.cmf.FeatureManager;
//import com.cloudera.server.cmf.LicenseManager;
//import com.cloudera.server.cmf.TrialManager;
//import com.cloudera.server.cmf.ClusterUsage.Product;
//import com.cloudera.server.cmf.LicenseManager.InstallResult;
//import com.cloudera.server.web.cmf.MessageException;
//import com.cloudera.server.web.common.I18n;
//import com.google.common.collect.ImmutableMap;
//import com.google.common.collect.ImmutableMap.Builder;
//
//import java.util.*;
//import java.util.stream.Collectors;
//import org.joda.time.DateTime;
//import org.joda.time.DateTimeZone;
//
//public class LicenseManagerDaoImpl implements LicenseManagerDao {
//    private static final String LABEL_FEATURE = "label.feature.%s";
//    private final TrialManager trialMgr;
//    private final LicenseManager licenseMgr;
//    private final FeatureManager featureMgr;
//
//    public LicenseManagerDaoImpl(TrialManager trialMgr, LicenseManager licenseMgr, ServiceDataProvider sdp) {
//        this.trialMgr = trialMgr;
//        this.licenseMgr = licenseMgr;
//        this.featureMgr = sdp.getFeatureManager();
//    }
//
//    private ApiLicense newLicense(License lic) {
//        DateTime expiration = lic.getExpirationDate();
//        Date apiExpiration = null;
//        if (expiration != null) {
//            expiration = expiration.withZoneRetainFields(DateTimeZone.UTC);
//            apiExpiration = new Date(expiration.getMillis());
//        }
//
//        ApiLicense result = new ApiLicense(lic.getName(), lic.getUUID().toString(), apiExpiration);
//        if (ApiVersionContext.getVersion() >= 32) {
//            List<ApiLicenseFeature> features = (List)EnumSet.allOf(Feature.class).stream().filter(ProductState::hasFeature).map((f) -> {
//                return this.createApiLicenseFeature(f);
//            }).collect(Collectors.toList());
//            result.setFeatures(features);
//            DateTime deactivationDate;
//            if (lic.getStartDate() != null) {
//                deactivationDate = lic.getStartDate().withZoneRetainFields(DateTimeZone.UTC);
//                result.setStartDate(new Date(deactivationDate.getMillis()));
//            }
//
//            if (lic.getDeactivationDate() != null) {
//                deactivationDate = lic.getDeactivationDate().withZoneRetainFields(DateTimeZone.UTC);
//                result.setDeactivationDate(new Date(deactivationDate.getMillis()));
//            }
//        }
//
//        return result;
//    }
//
//    public ApiLicense readLicense() {
//        //License lic = LicenseData.getLicense();
//        License lic = new TrialLicense(new DateTime(2020,8,18,10,58),new DateTime(2099,8,18,10,58));
//
//        if (lic == null) {
//            throw new NoSuchElementException(I18n.t("message.license.noLicense"));
//        } else {
//            return this.newLicense(lic);
//        }
//
//    }
//
//    public ApiLicense updateLicense(byte[] licenseData) {
//        InstallResult res = null;
//
//        try {
//            res = this.licenseMgr.installLicense(licenseData, false);
//        } catch (MessageException var4) {
//            throw new IllegalArgumentException(var4.getMessage());
//        }
//
//        if (res.error) {
//            throw new IllegalArgumentException(res.msg);
//        } else {
//            return this.newLicense(res.license);
//        }
//    }
//
//    public void beginTrial() {
//        this.trialMgr.begin();
//    }
//
//    public void endTrial() {
//        this.trialMgr.end();
//    }
//
//    public ApiLicensedFeatureUsage getLicensedFeatureUsage() {
//        ClusterUsageCollection cuc = this.licenseMgr.getLicensedFeatureUsage();
//        Builder<String, Integer> totals = ImmutableMap.builder();
//        Product[] var3 = Product.values();
//        int var4 = var3.length;
//
//        for(int var5 = 0; var5 < var4; ++var5) {
//            Product p = var3[var5];
//            totals.put(p.toString(), cuc.getTotalUsageForProduct(p));
//        }
//
//        Builder<String, Map<String, Integer>> clusters = ImmutableMap.builder();
//        Iterator var12 = cuc.getClusterUsages().iterator();
//
//        while(var12.hasNext()) {
//            ClusterUsage cu = (ClusterUsage)var12.next();
//            Builder<String, Integer> cb = ImmutableMap.builder();
//            Product[] var7 = Product.values();
//            int var8 = var7.length;
//
//            for(int var9 = 0; var9 < var8; ++var9) {
//                Product p = var7[var9];
//                cb.put(p.toString(), cu.getUsage(p));
//            }
//
//            clusters.put(cu.getName(), cb.build());
//        }
//
//        return new ApiLicensedFeatureUsage(totals.build(), clusters.build());
//    }
//
//    private ApiLicenseFeature createApiLicenseFeature(Feature f) {
//        return new ApiLicenseFeature(f.name(), I18n.t(String.format("label.feature.%s", f.name().toLowerCase())), this.featureMgr.hasFeature(f) && f.isEnabled());
//    }
//}
